home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / src / exampleCode / opengl / GLUT / lib / fglut / glut_overlay.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-11-11  |  14.8 KB  |  479 lines

  1. /* Copyright (c) Mark J. Kilgard, 1996.  */
  2.  
  3. /* This program is freely distributable without licensing fees
  4.    and is provided without guarantee or warrantee expressed or
  5.    implied. This program is -not- in the public domain. */
  6.  
  7. #include <stdlib.h>
  8. #include <stdio.h>
  9. #include <string.h>
  10. #include <assert.h>
  11. #include <X11/Xlib.h>
  12. #include <X11/Xutil.h>
  13. #include <X11/Xatom.h>  /* for XA_RGB_DEFAULT_MAP atom */
  14. #if defined (__vms)
  15. #include <X11/StdCmap.h>  /* for XmuLookupStandardColormap */
  16. #else
  17. #include <X11/Xmu/StdCmap.h>  /* for XmuLookupStandardColormap */
  18. #endif
  19.  
  20. #include <GL/glut.h>
  21. #include "glutint.h"
  22. #include "layerutil.h"
  23.  
  24. static int
  25. checkOverlayAcceptability(XVisualInfo * vi, unsigned int mode)
  26. {
  27.   int value;
  28.  
  29.   /* Must support OpenGL. */
  30.   glXGetConfig(__glutDisplay, vi, GLX_USE_GL, &value);
  31.   if (!value)
  32.     return 1;
  33.  
  34.   /* Must be color index. */
  35.   glXGetConfig(__glutDisplay, vi, GLX_RGBA, &value);
  36.   if (value)
  37.     return 1;
  38.  
  39.   /* Must match single/double buffering request. */
  40.   glXGetConfig(__glutDisplay, vi, GLX_DOUBLEBUFFER, &value);
  41.   if (GLUT_WIND_IS_DOUBLE(mode) != (value != 0))
  42.     return 1;
  43.  
  44.   /* Must match mono/stereo request. */
  45.   glXGetConfig(__glutDisplay, vi, GLX_STEREO, &value);
  46.   if (GLUT_WIND_IS_STEREO(mode) != (value != 0))
  47.     return 1;
  48.  
  49.   /* Alpha and accumulation buffers incompatible with color
  50.      index. */
  51.   if (GLUT_WIND_HAS_ALPHA(mode) || GLUT_WIND_HAS_ACCUM(mode))
  52.     return 1;
  53.  
  54.   /* Look for depth buffer if requested. */
  55.   glXGetConfig(__glutDisplay, vi, GLX_DEPTH_SIZE, &value);
  56.   if (GLUT_WIND_HAS_DEPTH(mode) && (value <= 0))
  57.     return 1;
  58.  
  59.   /* Look for stencil buffer if requested. */
  60.   glXGetConfig(__glutDisplay, vi, GLX_STENCIL_SIZE, &value);
  61.   if (GLUT_WIND_HAS_STENCIL(mode) && (value <= 0))
  62.     return 1;
  63.  
  64. #if defined(GLX_VERSION_1_1) && defined(GLX_SGIS_multisample)
  65.   /* XXX Multisampled overlay color index??  Pretty unlikely. */
  66.   /* Look for multisampling if requested. */
  67.   if (__glutIsSupportedByGLX("GLX_SGIS_multisample"))
  68.     glXGetConfig(__glutDisplay, vi, GLX_SAMPLES_SGIS, &value);
  69.   else
  70.     value = 0;
  71.   if (GLUT_WIND_IS_MULTISAMPLE(mode) && (value <= 0))
  72.     return 1;
  73. #endif
  74.  
  75.   return 0;
  76. }
  77.  
  78. static XVisualInfo *
  79. getOverlayVisualInfoCI(unsigned int mode)
  80. {
  81.   XLayerVisualInfo *vi;
  82.   XLayerVisualInfo template;
  83.   XVisualInfo *goodVisual, *returnVisual;
  84.   int nitems, i, j, bad;
  85.  
  86.   /* The GLX 1.0 glXChooseVisual is does not permit queries
  87.      based on pixel transparency (and GLX_BUFFER_SIZE uses
  88.      "smallest that meets" its requirement instead of "largest
  89.      that meets" that GLUT wants. So, GLUT implements its own
  90.      visual selection routine for color index overlays. */
  91.  
  92.   /* Try three overlay layers. */
  93.   for (i = 1; i <= 3; i++) {
  94.     template.vinfo.screen = __glutScreen;
  95.     template.vinfo.class = PseudoColor;
  96.     template.layer = i;
  97.     template.type = TransparentPixel;
  98.     vi = __glutXGetLayerVisualInfo(__glutDisplay,
  99.       VisualTransparentType | VisualScreenMask | VisualClassMask | VisualLayerMask,
  100.       &template, &nitems);
  101.     if (vi) {
  102.       /* Check list for acceptable visual meeting requirements
  103.          of requested display mode. */
  104.       for (j = 0; j < nitems; j++) {
  105.         bad = checkOverlayAcceptability(&vi[j].vinfo, mode);
  106.         if (bad) {
  107.           /* Set vi[j].vinfo.visual to mark it unacceptable. */
  108.           vi[j].vinfo.visual = NULL;
  109.         }
  110.       }
  111.  
  112.       /* Look through list to find deepest acceptable visual. */
  113.       goodVisual = NULL;
  114.       for (j = 0; j < nitems; j++) {
  115.         if (vi[j].vinfo.visual) {
  116.           if (goodVisual == NULL) {
  117.             goodVisual = &vi[j].vinfo;
  118.           } else {
  119.             if (goodVisual->depth < vi[j].vinfo.depth) {
  120.               goodVisual = &vi[j].vinfo;
  121.             }
  122.           }
  123.         }
  124.       }
  125.  
  126.       /* If a visual is found, clean up and return the visual. */
  127.       if (goodVisual) {
  128.         returnVisual = (XVisualInfo *) malloc(sizeof(XVisualInfo));
  129.         if (returnVisual) {
  130.           *returnVisual = *goodVisual;
  131.         }
  132.         XFree(vi);
  133.         return returnVisual;
  134.       }
  135.       XFree(vi);
  136.     }
  137.   }
  138.   return NULL;
  139. }
  140.  
  141. static XVisualInfo *
  142. getOverlayVisualInfoRGB(unsigned int mode)
  143. {
  144.  
  145.   /* XXX For now, transparent RGBA overlays are not supported
  146.      by GLUT.  RGBA overlays raise difficult questions about
  147.      what the transparent pixel (really color) value should be.
  148.  
  149.      Color index overlay transparency is "easy" because the
  150.      transparent pixel value does not affect displayable colors
  151.      (except for stealing one color cell) since colors are
  152.      determined by indirection through a colormap, and because
  153.      it is uncommon for arbitrary pixel values in color index to
  154.      be "calculated" (as can occur with a host of RGBA operations
  155.      like lighting, blending, etc) so it is easy to avoid the
  156.      transparent pixel value.
  157.  
  158.      Since it is typically easy to avoid the transparent pixel
  159.      value in color index mode, if GLUT tells the programmer what
  160.      pixel is transparent, then most program can easily avoid
  161.      generating that pixel value except when they intend
  162.      transparency.  GLUT returns whatever transparent pixel value
  163.      is provided by the system through glutGet(
  164.      GLUT_TRANSPARENT_INDEX).
  165.  
  166.      Theory versus practice for RGBA overlay transparency: In
  167.      theory, the reasonable thing is enabling overlay transparency 
  168.  
  169.      when an overlay pixel's destination alpha is 0 because this
  170.      allows overlay transparency to be controlled via alpha and
  171.      all visibile colors are permited, but no hardware I am aware
  172.      of supports this practice (and it requires destination alpha
  173.      which is typically optional and quite uncommon for overlay
  174.      windows!).  In practice, the choice of  transparent pixel
  175.      value is typically "hardwired" into most graphics hardware to 
  176.  
  177.      a single pixel value.  SGI hardware uses true black (0,0,0)
  178.      without regard for the destination alpha.  This is far from
  179.      ideal because true black (a common color that is easy to
  180.      accidently generate) can not be generated in an RGBA overlay. 
  181.  
  182.      I am not sure what other vendors do.
  183.  
  184.      Pragmatically, most of the typical things you want to do in
  185.      the overlays can be done in color index (rubber banding,
  186.      pop-up menus, etc.).  One solution for GLUT would be to
  187.      simply "advertise" what RGB triple (or possibly RGBA
  188.      quadruple or simply A alone) generates transparency.  The
  189.      problem with this approach is that it forces programmers to
  190.      avoid whatever arbitrary color various systems decide is
  191.      transparent.  This is a difficult burden to place on
  192.      programmers that want to portably make use of overlays.
  193.  
  194.      To actually support transparent RGBA overlays, there are
  195.      really two reaonsable options.  ONE: Simply mandate that true 
  196.  
  197.      black is the RGBA overlay transparent color (what IRIS GL
  198.      did).  This is nice for programmers since only one option,
  199.      nice for existing SGI hardware, bad for anyone (including
  200.      SGI) who wants to improve upon "true black" RGB transparency. 
  201.  
  202.      Or TWO: Provide a set of queriable "transparency types" (like 
  203.  
  204.      "true black" or "alpha == 0" or "true white" or even a
  205.      queriable transparent color).  This is harder for
  206.      programmers, OK for existing SGI hardware, and it leaves open 
  207.  
  208.      the issue of what other modes are reasonable.
  209.  
  210.      Option TWO seems the more general approach, but since
  211.      hardware designers will likely only implement a single mode
  212.      (this is a scan out issue where bandwidth is pressing issue),
  213.      codifying multiple speculative approaches nobody may ever
  214.      implement seems silly.  And option ONE fiats a suboptimal
  215.      solution.
  216.  
  217.      Therefore, I defer any decision of how GLUT should support
  218.      RGBA overlay transparency and leave support for it
  219.      unimplemented. Nobody has been pressing me for RGBA overlay
  220.      transparency (though people have requested color index overlay 
  221.  
  222.      transparency repeatedly).  Geez, if you read this far you are
  223.      either really bored or maybe actually  interested in this
  224.      topic.  Anyway, if you have ideas (particularly if you plan on 
  225.  
  226.      implementing a hardware scheme for RGBA overlay transparency), 
  227.  
  228.      I'd be interested.
  229.  
  230.      For the record, SGI's expiremental Framebufer Configuration
  231.      experimental GLX extension uses option TWO.  Transparency
  232.      modes for "none" and "RGB" are defined (others could be
  233.      defined later).  What RGB value is the transparent one must be 
  234.  
  235.      queried. I was hoping GLUT could have something that required
  236.      less work from the programmer to use portably. -mjk */
  237.  
  238.   __glutWarning("RGBA overlays are not supported by GLUT (for now).");
  239.   return NULL;
  240. }
  241.  
  242. static XVisualInfo *
  243. getOverlayVisualInfo(unsigned int mode)
  244. {
  245.   /* XXX GLUT_LUMINANCE not implemented for GLUT 3.0. */
  246.   if (GLUT_WIND_IS_LUMINANCE(mode))
  247.     return NULL;
  248.  
  249.   if (GLUT_WIND_IS_RGB(mode))
  250.     return getOverlayVisualInfoRGB(mode);
  251.   else
  252.     return getOverlayVisualInfoCI(mode);
  253. }
  254.  
  255. static void
  256. addStaleWindow(GLUTwindow * window, Window win)
  257. {
  258.   GLUTstale *entry;
  259.  
  260.   entry = (GLUTstale *) malloc(sizeof(GLUTstale));
  261.   if (!entry)
  262.     __glutFatalError("out of memory");
  263.   entry->window = window;
  264.   entry->win = win;
  265.   entry->next = __glutStaleWindowList;
  266.   __glutStaleWindowList = entry;
  267. }
  268.  
  269. void
  270. __glutFreeOverlay(GLUToverlay * overlay)
  271. {
  272.   XFree(overlay->vis);
  273.   XDestroyWindow(__glutDisplay, overlay->win);
  274.   glXDestroyContext(__glutDisplay, overlay->ctx);
  275.   if (overlay->colormap) {
  276.     /* Only color index overlays have colormap data structure. */
  277.     __glutFreeColormap(overlay->colormap);
  278.   }
  279.   free(overlay);
  280. }
  281.  
  282. /* CENTRY */
  283. void
  284. glutEstablishOverlay(void)
  285. {
  286.   GLUToverlay *overlay;
  287.   GLUTwindow *window;
  288.   XSetWindowAttributes wa;
  289.  
  290.   /* Register a routine to free an overlay with glut_win.c;
  291.      this keeps glut_win.c from pulling in all of
  292.      glut_overlay.c when no overlay functionality is used by
  293.      the application. */
  294.   __glutFreeOverlayFunc = __glutFreeOverlay;
  295.  
  296.   window = __glutCurrentWindow;
  297.  
  298.   /* Allow for an existant overlay to be re-established perhaps
  299.      if you wanted a different display mode. */
  300.   if (window->overlay) {
  301.     addStaleWindow(window, window->overlay->win);
  302.     __glutFreeOverlay(window->overlay);
  303.   }
  304.   overlay = (GLUToverlay *) malloc(sizeof(GLUToverlay));
  305.   if (!overlay)
  306.     __glutFatalError("out of memory.");
  307.  
  308.   overlay->vis = __glutDetermineVisual(__glutDisplayMode,
  309.     &overlay->fakeSingle, getOverlayVisualInfo);
  310.   if (!overlay->vis) {
  311.     __glutFatalError("lacks overlay support.");
  312.   }
  313.   overlay->ctx = glXCreateContext(__glutDisplay, overlay->vis,
  314.     None, __glutTryDirect);
  315.   overlay->isDirect = glXIsDirect(__glutDisplay, overlay->ctx);
  316.   if (__glutForceDirect) {
  317.     if (!overlay->isDirect) {
  318.       __glutFatalError("direct rendering not possible.");
  319.     }
  320.   }
  321.   __glutSetupColormap(overlay->vis, &(overlay->colormap), &(overlay->cmap));
  322.   overlay->transparentPixel = __glutGetTransparentPixel(__glutDisplay,
  323.     overlay->vis);
  324.  
  325.   wa.colormap = overlay->cmap;
  326.   wa.background_pixel = overlay->transparentPixel;
  327.   wa.event_mask = window->eventMask & GLUT_OVERLAY_EVENT_FILTER_MASK;
  328.   wa.border_pixel = 0;
  329.   overlay->win = XCreateWindow(__glutDisplay,
  330.     window->win,
  331.     0, 0, window->width, window->height, 0,
  332.     overlay->vis->depth, InputOutput, overlay->vis->visual,
  333.     CWBackPixel | CWBorderPixel | CWEventMask | CWColormap,
  334.     &wa);
  335.   if (window->children) {
  336.     /* Overlay window must be lowered below any GLUT
  337.        subwindows. */
  338.     XLowerWindow(__glutDisplay, overlay->win);
  339.   }
  340.   XMapWindow(__glutDisplay, overlay->win);
  341.   overlay->shownState = 1;
  342.  
  343.   overlay->damaged = 0;
  344.   overlay->display = NULL;
  345.  
  346.   /* Make sure a reshape gets delivered. */
  347.   window->forceReshape = True;
  348.  
  349.   __glutPutOnWorkList(__glutToplevelOf(window), GLUT_COLORMAP_WORK);
  350.  
  351.   window->overlay = overlay;
  352.   glutUseLayer(GLUT_OVERLAY);
  353. }
  354.  
  355. void
  356. glutRemoveOverlay(void)
  357. {
  358.   GLUTwindow *window = __glutCurrentWindow;
  359.   GLUToverlay *overlay = __glutCurrentWindow->overlay;
  360.  
  361.   if (!window->overlay)
  362.     return;
  363.  
  364.   /* If using overlay, switch to the normal layer. */
  365.   if (window->renderWin == overlay->win) {
  366.     glutUseLayer(GLUT_NORMAL);
  367.   }
  368.   addStaleWindow(window, overlay->win);
  369.   __glutFreeOverlay(overlay);
  370.   window->overlay = NULL;
  371.   __glutPutOnWorkList(__glutToplevelOf(window), GLUT_COLORMAP_WORK);
  372. }
  373.  
  374. void
  375. glutUseLayer(GLenum layer)
  376. {
  377.   GLUTwindow *window = __glutCurrentWindow;
  378.  
  379.   switch (layer) {
  380.   case GLUT_NORMAL:
  381.     window->renderWin = window->win;
  382.     window->renderCtx = window->ctx;
  383.     break;
  384.   case GLUT_OVERLAY:
  385.     window->renderWin = window->overlay->win;
  386.     window->renderCtx = window->overlay->ctx;
  387.     break;
  388.   default:
  389.     __glutWarning("glutUseLayer: unknown layer, %d.", layer);
  390.     break;
  391.   }
  392.   __glutSetWindow(window);
  393. }
  394.  
  395. void
  396. glutPostOverlayRedisplay(void)
  397. {
  398.   __glutPostRedisplay(__glutCurrentWindow, GLUT_OVERLAY_REDISPLAY_WORK);
  399. }
  400.  
  401. void
  402. glutOverlayDisplayFunc(GLUTdisplayCB displayFunc)
  403. {
  404.   if (!__glutCurrentWindow->overlay) {
  405.     __glutWarning("glutOverlayDisplayFunc: window has no overlay established");
  406.     return;
  407.   }
  408.   __glutCurrentWindow->overlay->display = displayFunc;
  409. }
  410.  
  411. void
  412. glutHideOverlay(void)
  413. {
  414.   if (!__glutCurrentWindow->overlay) {
  415.     __glutWarning("glutHideOverlay: window has no overlay established");
  416.     return;
  417.   }
  418.   XUnmapWindow(__glutDisplay, __glutCurrentWindow->overlay->win);
  419.   __glutCurrentWindow->overlay->shownState = 0;
  420. }
  421.  
  422. void
  423. glutShowOverlay(void)
  424. {
  425.   if (!__glutCurrentWindow->overlay) {
  426.     __glutWarning("glutShowOverlay: window has no overlay established");
  427.     return;
  428.   }
  429.   XMapWindow(__glutDisplay, __glutCurrentWindow->overlay->win);
  430.   __glutCurrentWindow->overlay->shownState = 1;
  431. }
  432.  
  433. int
  434. glutLayerGet(GLenum param)
  435. {
  436.   switch (param) {
  437.   case GLUT_OVERLAY_POSSIBLE:
  438.     {
  439.       XVisualInfo *vi;
  440.       Bool dummy;
  441.  
  442.       vi = __glutDetermineVisual(__glutDisplayMode, &dummy, getOverlayVisualInfo);
  443.       if (vi) {
  444.         XFree(vi);
  445.         return 1;
  446.       }
  447.       return 0;
  448.     }
  449.   case GLUT_LAYER_IN_USE:
  450.     return __glutCurrentWindow->renderWin != __glutCurrentWindow->win;
  451.   case GLUT_HAS_OVERLAY:
  452.     return __glutCurrentWindow->overlay != NULL;
  453.   case GLUT_TRANSPARENT_INDEX:
  454.     if (__glutCurrentWindow->overlay) {
  455.       return __glutCurrentWindow->overlay->transparentPixel;
  456.     } else {
  457.       return -1;
  458.     }
  459.   case GLUT_NORMAL_DAMAGED:
  460.     /* __glutWindowDamaged is used so the damage state within
  461.        the window (or overlay belwo) can be cleared before
  462.        calling a display callback so on return, the state does
  463.        not have to be cleared (since upon return from the
  464.        callback the window could be destroyed (or layer
  465.        removed). */
  466.     return __glutCurrentWindow->damaged || __glutWindowDamaged;
  467.   case GLUT_OVERLAY_DAMAGED:
  468.     if (__glutCurrentWindow->overlay) {
  469.       return __glutCurrentWindow->overlay->damaged || __glutWindowDamaged;
  470.     } else {
  471.       return -1;
  472.     }
  473.   default:
  474.     __glutWarning("invalid glutLayerGet param: %d", param);
  475.     return -1;
  476.   }
  477. }
  478. /* ENDCENTRY */
  479.